package com.taoz.rpc.core.serializer;


import java.io.*;

public class DefaultSerializer implements Serializer {

    @Override
    public <T> byte[] serialize(T o) throws IOException {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
            objectOutputStream.writeObject(o);
            return outputStream.toByteArray();
        }
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clazz) throws IOException, ClassNotFoundException {
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
            Object o = objectInputStream.readObject();
            return (T) o;
        }
    }
}
